package com.fancy.music.controller;

import com.fancy.common.dto.AlbumBasicInfo;
import com.fancy.common.dto.PageResult;
import com.fancy.common.dto.response.Result;
import com.fancy.music.dto.AlbumDetailDTO;
import com.fancy.music.entity.Album;
import com.fancy.music.service.AlbumService;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

@Api(tags = "专辑管理")
@RestController
@RequestMapping("/api/v1/albums")
@RequiredArgsConstructor
public class AlbumController {

    private final AlbumService albumService;

    @ApiOperation(value = "搜索专辑", notes = "根据关键词、风格、语言等条件搜索专辑")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "keyword", value = "搜索关键词", paramType = "query", dataType = "string", example = "七里香"),
        @ApiImplicitParam(name = "genre", value = "音乐风格", paramType = "query", dataType = "string", example = "流行"),
        @ApiImplicitParam(name = "language", value = "语言", paramType = "query", dataType = "string", example = "中文"),
        @ApiImplicitParam(name = "page", value = "页码", paramType = "query", dataType = "int", example = "1"),
        @ApiImplicitParam(name = "size", value = "每页大小", paramType = "query", dataType = "int", example = "20")
    })
    @GetMapping("/search")
    public Result<PageResult<Album>> searchAlbums(
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) String genre,
            @RequestParam(required = false) String language,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "20") int size) {
        PageResult<Album> result = albumService.searchAlbums(keyword, genre, language, page, size);
        return Result.success(result);
    }

    @ApiOperation(value = "获取专辑详情", notes = "根据专辑ID获取专辑的详细信息")
    @ApiImplicitParam(name = "albumId", value = "专辑ID", required = true, paramType = "path", dataType = "long", example = "1")
    @GetMapping("/{albumId}")
    public Result<AlbumDetailDTO> getAlbumDetail(@PathVariable Long albumId) {
        AlbumDetailDTO album = albumService.getAlbumDetailWithInfo(albumId);
        if (album != null) {
            return Result.success(album);
        } else {
            return Result.fail("专辑不存在");
        }
    }

    @ApiOperation(value = "获取热门专辑", notes = "获取热门专辑列表")
    @ApiImplicitParam(name = "limit", value = "返回数量限制", paramType = "query", dataType = "int", example = "10")
    @GetMapping("/hot")
    public Result<List<AlbumDetailDTO>> getHotAlbums(@RequestParam(defaultValue = "10") Integer limit) {
        List<AlbumDetailDTO> albums = albumService.getHotAlbumsWithDetails(limit);
        return Result.success(albums);
    }

    @ApiOperation(value = "获取新专辑", notes = "获取最新发布的专辑列表")
    @ApiImplicitParam(name = "limit", value = "返回数量限制", paramType = "query", dataType = "int", example = "10")
    @GetMapping("/new")
    public Result<List<AlbumDetailDTO>> getNewAlbums(@RequestParam(defaultValue = "10") Integer limit) {
        List<AlbumDetailDTO> albums = albumService.getNewAlbumsWithDetails(limit);
        return Result.success(albums);
    }

    @Deprecated
    @ApiOperation(value = "按风格获取专辑", notes = "根据音乐风格获取专辑列表")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "genre", value = "音乐风格", required = true, paramType = "query", dataType = "string", example = "流行"),
        @ApiImplicitParam(name = "limit", value = "返回数量限制", paramType = "query", dataType = "int", example = "10")
    })
    @GetMapping("/by-genre")
    public Result<List<AlbumDetailDTO>> getAlbumsByGenre(
            @RequestParam String genre,
            @RequestParam(defaultValue = "10") Integer limit) {
        List<AlbumDetailDTO> albums = albumService.getAlbumsByGenreWithDetails(genre, limit);
        return Result.success(albums);
    }

    @ApiOperation(value = "根据歌手获取专辑", notes = "获取指定歌手的所有专辑")
    @ApiImplicitParam(name = "artistId", value = "歌手ID", required = true, paramType = "path", dataType = "long", example = "1")
    @GetMapping("/artist/{artistId}")
    public Result<List<AlbumDetailDTO>> getAlbumsByArtist(@PathVariable Long artistId) {
        List<AlbumDetailDTO> albums = albumService.getAlbumsByArtistIdWithDetails(artistId);
        return Result.success(albums);
    }

    // 内部接口，不显示在API文档中
    @ApiIgnore
    @GetMapping("/internal/{albumId}")
    public Result<AlbumBasicInfo> getAlbumBasicInfo(@PathVariable Long albumId) {
        AlbumBasicInfo info = albumService.getAlbumBasicInfo(albumId);
        return Result.success(info);
    }

    @ApiIgnore
    @PostMapping("/internal/batch")
    public Result<List<AlbumBasicInfo>> getAlbumBasicInfoBatch(@RequestBody List<Long> albumIds) {
        List<AlbumBasicInfo> infos = albumService.getAlbumBasicInfoBatch(albumIds);
        return Result.success(infos);
    }

    @ApiOperation(value = "获取所有专辑音乐风格", notes = "获取系统中所有专辑的音乐风格标签")
    @GetMapping("/genres")
    public Result<List<String>> getAllAlbumGenres() {
        List<String> genres = albumService.getAllAlbumGenres();
        return Result.success(genres);
    }

    @ApiOperation(value = "根据音乐风格获取专辑", notes = "根据音乐风格获取匹配的专辑列表（支持部分匹配）")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "genre", value = "音乐风格", required = true, paramType = "query", dataType = "string", example = "华语流行"),
        @ApiImplicitParam(name = "limit", value = "返回数量限制", paramType = "query", dataType = "int", example = "10")
    })
    @GetMapping("/by-genre-match")
    public Result<List<AlbumDetailDTO>> getAlbumsByGenreMatch(
            @RequestParam String genre,
            @RequestParam(defaultValue = "10") Integer limit) {
        List<AlbumDetailDTO> albums = albumService.getAlbumsByGenreMatchWithDetails(genre, limit);
        return Result.success(albums);
    }
}